package com.spring.core.xml;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import com.spring.core.model.SpringMappings;

/**
 * @Author 琦哥 110
 * @Date 2018/7/5 10:49
 */
public class XmlScan {
	public  static SpringMappings configure(String path) {
		InputStream in = XmlScan.class.getClassLoader().getResourceAsStream(path);
		SAXBuilder builder = new SAXBuilder();
		SpringMappings mapping = new SpringMappings();
		try {
			Document document = builder.build(in);
			Element root = document.getRootElement();
			List<Element> children = root.getChildren();
			for (Element child : children) {
				if (child.getName().equals("component-scan")) {
					mapping.setBasePackage(child.getAttributeValue("base-package"));
				}
				if (child.getName().equals("bean")) {
					if (child.getAttributeValue("class") != null) {
						if (child.getAttributeValue("class").contains("InternalResourceViewResolver")) {
							List<Element> pre = child.getChildren();
							for (Element property : pre) {
								if (property.getAttribute("name").getValue().equals("prefix")) {
									mapping.setPrefix(property.getAttribute("value").getValue());
								}
								if (property.getAttribute("name").getValue().equals("suffix")) {
									mapping.setSuffix(property.getAttribute("value").getValue());
								}

							}
						}

					}
				}

			}

		} catch (Exception e) {

			e.printStackTrace();
		}finally{
			try {
				in.close();
			} catch (IOException e) {
				
				e.printStackTrace();
			}
		}
		return mapping;
	}

public static void main(String[] args) {
	System.out.println(configure("applicationContext.xml"));
}
}
